﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;
using System.IO;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for AvatarsRepositoryTest and is intended
    ///to contain all AvatarsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AvatarsRepositoryTest : BaseTestClass
    {
        #region Fields
        private List<Avatar> _avatars;
        private AvatarsFactory _avatarsFactory;
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private static List<string> _images;
        #endregion

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            _images = new List<string>{ "1.jpg","2.jpg","3.jpg"};

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(10);
            _users.ForEach(user => usersRepository.Add(user));

            var storageContext = _unityContainer.Resolve<IStorageServiceContext<Avatar>>();
            _avatarsFactory = new AvatarsFactory(storageContext.BaseURL);

            var avatarsrepository = _unityContainer.Resolve<IAvatarsRepository>();
            _avatars = _avatarsFactory.CreateList(_users, _images[1]);
            avatarsrepository.Add(_avatars[1]);
            avatarsrepository.Add(_avatars[2]);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var avatarsrepository = _unityContainer.Resolve<IAvatarsRepository>();
            usersRepository.DeleteTestEntities();

            _avatars.ForEach(a => avatarsrepository.Delete(a));
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void AddTest()
        {
            var target = _unityContainer.Resolve<AvatarsRepository>();
            target.Add(_avatars[0]);

            var url = target.GetUrl(_avatars[0]);
            Assert.AreEqual(_avatars[0].Url, url);
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest()
        {
            var target = _unityContainer.Resolve<AvatarsRepository>();
            var entity = _avatars[1];
            target.Delete(entity);

            var exists = target.Exists(entity);
            Assert.IsFalse(exists);
        }

        /// <summary>
        ///A test for GetUrl
        ///</summary>
        [TestMethod()]
        public void GetUrlTest()
        {
            var target = _unityContainer.Resolve<AvatarsRepository>();
            var entity = _avatars[2];

            var url = target.GetUrl(entity);
            Assert.AreEqual(entity.Url, url);
        }
    }
}
